#include "Stack.h"

void STInit(ST* pst)
{
	assert(pst);
	pst->top = 0;
	pst->capacity = 0;
	pst->arr = NULL;
}

void STDestroy(ST* pst)
{
	assert(pst);
	pst->capacity = pst->top = 0;
	free(pst->arr);
	pst->arr = NULL;
}

void STPush(ST* pst,STDataType x)
{
	assert(pst);
	if (pst->top == pst ->capacity)
	{
		int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;
		STDataType* temp = (STDataType*)realloc(pst->arr, newcapacity * sizeof(STDataType));
		if (NULL == temp)
		{
			perror("realloc fail");
			exit(1);
		}
		pst->capacity = newcapacity;
		pst->arr = temp;
	}
	pst->arr[pst->top] = x;
	pst->top++;
}

void STPop(ST* pst)
{
	assert(pst);
	assert(pst->top);
	pst->top--;
}

STDataType STTop(ST* pst)
{
	assert(pst);
	assert(pst->top);
	return pst->arr[pst->top - 1];
}

bool STEmpty(ST* pst)
{
	assert(pst);
	return !pst->top;
}

int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}

